﻿@{
    ViewBag.Title = "User Role Management";
    
}
<br />
<h2>User Role Management</h2>
<br />
@(Html.Telerik().Grid<Century.Model.UserProfile>()
    .Name("Grid")
    .DataKeys(keys => keys.Add(c => c.UserId))
    //.ToolBar(c =>
    //{
    //    c.Insert().ButtonType(GridButtonType.Image).ImageHtmlAttributes(new { style = "margin-left:0" });
    //})
    .Columns(columns =>
    {

        columns.Bound(o => o.UserId).Hidden().ReadOnly(true);
        columns.Bound(o => o.UserName);
        columns.Bound(o => o.Email);
        columns.Bound(o => o.TitleId);
        columns.Bound(o => o.FirstName);
        columns.Bound(o => o.LastName);
        columns.Bound(o => o.CellPhone);
        //columns.Command(command => { command.Edit().ButtonType(GridButtonType.Image); }).Width(50);
        //columns.Command(command =>
        //{
        //    command.Custom("NewPassWord").Text("New Pass").ButtonType(GridButtonType.Image).HtmlAttributes(new { @class = "t-newpassword", alt="New Password" })
        //    .DataRouteValues(route => route.Add(o => o.UserName).RouteKey("userName"))
        //    .Action("ChangePasswordByAdmin", "UserRoleManagement");
        //}).Width(50);
    })
    //.DetailView(details => details.ClientTemplate(
    //    Html.Telerik().Grid<Century.Models.Common.Role>()
    //        .DataKeys(keys => keys.Add(c => c.Id))
    //        .Name("Roles_<#= Id #>")
    //        .Columns(columns =>
    //        {
    //            columns.Bound(o => o.Id);
    //            columns.Bound(o => o.RoleName);
    //            columns.Bound(o => o.DisplayName);
    //            columns.Bound(o => o.UserHasThisRole)
    //            .ClientTemplate("<input type='checkbox' name='UserHasThisRole' value='<#= Id #>'  <#= UserHasThisRole?'checked':'' #> onchange = 'ChangeUserRole(this)' />");
    //        })
    //        .DataBinding(dataBinding => dataBinding.Ajax().Select("GetRolesForUser", "UserRoleManagement", new { userId = "<#= Id #>" }))
    //                    .Sortable()
    //                    .Filterable()
    //                    .ToHtmlString()
    // ))
        .DataBinding(dataBinding => dataBinding.Ajax()
    //    .Insert("AddUser", "UserRoleManagement")
        .Select("GetUserList", "UserRoleManagement")
    //    .Update("EditUser", "UserRoleManagement")
    )
    .Groupable()
    .Filterable()
    //.Editable(editing => editing.Mode(GridEditMode.InLine))
    .Sortable(sortable => sortable.OrderBy(order => order.Add(o => o.UserName)))
    .Pageable(pagingSetting => pagingSetting.PageSize(20))
    .Resizable(config => { config.Columns(true); })
    .Reorderable(config => { config.Columns(true); })
    .ColumnContextMenu()
)
<script type="text/javascript">
    function ChangeUserRole(sender) {
        $.ajax({
            type: "post",
            url: '@Url.Action("ChangeUserRole", "UserRoleManagement")',
            data: { userId: sender.parentElement.parentElement.parentElement.parentElement.parentElement.id.split('_')[1],
                roleId: sender.value,
                userHasThisRole: sender.checked
            },
            dataType: "html",
            success: function (msg) {
                alert('User role has been changed successfully');
            },
            error: function (msg) {
                alert('Error on changin user role');
            }
        });
    }

    function ChangeRoleActionModule(sender) {
         $.ajax({
                type: "post",
                url: '@Url.Action("ChangeRoleActionModule", "UserRoleManagement")',
                data: { roleId: sender.parentElement.parentElement.parentElement.parentElement.parentElement.id.split('_')[1],
                    ActionModuleId: sender.value,
                    roleHasThisActionModule: sender.checked
                },
                dataType: "html",
                success: function (msg) {
                    alert('Role ActionModule has been changed successfully');
                },
                error: function (msg) {
                    alert('Error on changin Role ActionModule');
                }
            });
    }
   
</script>
